﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QianBuo.Lottery.Server
{
    public partial class RegestForm : Form
    {
        public RegestForm()
        {
            InitializeComponent();
        }

        private void btnRegest_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtDNSName.Text))
            {
                MessageBox.Show("请输入域名。");
                return;
            }
            if (string.IsNullOrEmpty(txtRegestCode.Text))
            {
                MessageBox.Show("请输入注册码。");
                return;
            }

            bool flag = false;
            if (ckbClear.Checked)
            {
                DialogResult result= MessageBox.Show("您确定要删除之前所有已注册的域名?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                flag = result == DialogResult.Yes;
            }
            try
            {
                int count=new AccessData().Regest_Update(txtDNSName.Text,txtRegestCode.Text,flag);
                if (count > 0)
                    MessageBox.Show("注册成功！");
                else
                    MessageBox.Show("操作失败！请稍后重新操作。");
            }
            catch (AccessDataException ae)
            {
                MessageBox.Show(ae.Message);
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            var item = cbbDNSName.SelectedItem as KeyValuePair<string,string>?;
            if (item == null)
                return;
            var dnsName = item.Value.Key;
            var regestCode = item.Value.Value;
            var count = new AccessData().Regest_Delete(dnsName, regestCode);
            if (count > 0)
            {
                MessageBox.Show("删除成功！");
                cbbDNSName_DataBind();
            }
            else
                MessageBox.Show("操作失败！");
        }

        private void cbbDNSName_DropDown(object sender, EventArgs e)
        {
            if (cbbDNSName.DataSource != null) 
                return;
            cbbDNSName_DataBind();
        }
        private void cbbDNSName_DataBind()
        {
            cbbDNSName.DataSource = null;
            cbbDNSName.ValueMember = "Value";
            cbbDNSName.DisplayMember = "Key";
            cbbDNSName.DataSource = new AccessData().Regest_Get().ToList();
        }

        private void btnRef_Click(object sender, EventArgs e)
        {
            cbbDNSName_DataBind();
        }
    }
}
